﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace PolePosition.v3.Desktop.Utility
{
    public partial class TabReports : UserControl
    {
        public TabReports()
        {
            InitializeComponent();
        }

        private string m_ReportBasePath;
        private int m_ProjectId;
        private int m_UtilityId;
        private Common.Enumerations.DataTypes m_ReportDataType;

        public void ShowReports(int projectId, string projectName, Common.Enumerations.DataTypes type)
        {
            panelExTitle.Text = projectName;
            m_ProjectId = projectId;
            m_ReportDataType = type;

            BindList();

            if (Program.LookupTableData == null)
                return;

            string _ReportServerUrl;
            _ReportServerUrl = Program.GetSettingValue(Common.Constants.Settings.ReportServerUrl);
            m_ReportBasePath = Program.GetSettingValue(Common.Constants.Settings.ReportFolder);
            if (!m_ReportBasePath.EndsWith("/"))
                m_ReportBasePath += "/";

            this.reportViewer1.ServerReport.ReportServerUrl = new System.Uri(_ReportServerUrl, System.UriKind.Absolute);

            string _ReportServerUser;
            _ReportServerUser = Program.GetSettingValue(Common.Constants.Settings.ReportServerUser);

            string _ReportServerPassword;
            _ReportServerPassword = Program.GetSettingValue(Common.Constants.Settings.ReportServerPassword);

            string _ReportServerDomain;
            _ReportServerDomain = Program.GetSettingValue(Common.Constants.Settings.ReportServerDomain);

            if (!string.IsNullOrEmpty(_ReportServerUser))
            {
                NetworkCredential _ReportCredential = new NetworkCredential(_ReportServerUser, _ReportServerPassword, _ReportServerDomain);
                reportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials = _ReportCredential;
            }
        }

        public void ShowReports(int utilityId, int projectId, string projectName, Common.Enumerations.DataTypes type)
        {
            m_UtilityId = utilityId;
            ShowReports(projectId, projectName, type);
        }

        private void BindList()
        {
            if (Program.LookupTableData == null)
                return;

            int? _ProjectTypeId = null;
            
            switch (m_ReportDataType)
            {
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionInspection:
                    _ProjectTypeId = 1;
                    
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit:
                    _ProjectTypeId = 2;
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsDistributionInspection:
                    _ProjectTypeId = 3;
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsDistributionGpsVisit:
                    _ProjectTypeId = 4;
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.None:
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsXmission:
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsDistribution:
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsMeters:
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueXmission:
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueDistribution:
                case PolePosition.v3.Common.Enumerations.DataTypes.Custom:
                    return;
                default:
                    break;
            }

            if (_ProjectTypeId != null)
            {
                var _AvailableProjectReports = from report in Program.LookupTableData.ReportsProject
                                               where !report.ProjectTypeFk.HasValue || report.ProjectTypeFk == _ProjectTypeId
                                               where report.Active == true
                                               orderby report.ReportDisplayName
                                               select report;

                comboBoxAvailableReports.DisplayMember = "Text";
                comboBoxAvailableReports.ValueMember = "Value";

                BindingList<Common.BindingLists.ListItem> _List;
                _List = Common.BindingLists.GetList(_AvailableProjectReports.ToList(),
                    EntityLibrary.ReportsProjectEnumerations.Columns.ReportDisplayName.ToString(),
                    EntityLibrary.ReportsProjectEnumerations.Columns.ReportFileName.ToString(), true);
                comboBoxAvailableReports.DataSource = _List;
            }
        
        }

        private void GetAssetReport()
        {
            //todo: build some asset reports and wire this up
        }

        private void GetProjectReport()
        {
            try
            {
                this.reportViewer1.ServerReport.ReportPath = string.Format("{0}{1}", m_ReportBasePath, comboBoxAvailableReports.SelectedValue.ToString());

                Microsoft.Reporting.WinForms.ReportParameterInfoCollection _Params;
                _Params = this.reportViewer1.ServerReport.GetParameters();

                if (_Params.Count() > 0)
                {
                    List<Microsoft.Reporting.WinForms.ReportParameter> _ReportParams;
                    _ReportParams = new List<Microsoft.Reporting.WinForms.ReportParameter>();

                    foreach (Microsoft.Reporting.WinForms.ReportParameterInfo _Param in _Params)
                    {
                        switch (_Param.Name)
                        {
                            case "ProjectId":
                                _ReportParams.Add(new Microsoft.Reporting.WinForms.ReportParameter(_Param.Name, m_ProjectId.ToString()));
                                break;

                            case "UtilityId":
                                _ReportParams.Add(new Microsoft.Reporting.WinForms.ReportParameter(_Param.Name, m_UtilityId.ToString()));
                                break;

                            // HACK: hard coding sundance until database is corrected.
                            case "VendorId":
                                _ReportParams.Add(new Microsoft.Reporting.WinForms.ReportParameter(_Param.Name, "3"));
                                break;

                            default:
                                break;
                        }
                    }

                    if (_ReportParams.Count() >  0)
                        this.reportViewer1.ServerReport.SetParameters(_ReportParams);
                }

                this.reportViewer1.RefreshReport();
                //this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);

            }
            catch (Exception ex)
            {
                //    MessageBox.Show("There was a problem loading report.  Please note following message and contact an administrator.\r\n\r\n" + ex.Message);
            }
        
        }

        private void comboBoxAvailableReports_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxAvailableReports.SelectedValue == null)
                buttonItemGo.Enabled = false;
            else
                buttonItemGo.Enabled = true;
        }

        private void buttonItemGo_Click(object sender, EventArgs e)
        {
            switch (m_ReportDataType)
            {
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionInspection:
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit:
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsDistributionInspection:
                case PolePosition.v3.Common.Enumerations.DataTypes.ProjectsDistributionGpsVisit:
                    GetProjectReport();
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsXmission:
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsDistribution:
                case PolePosition.v3.Common.Enumerations.DataTypes.AssetsMeters:
                    GetAssetReport();
                    break;
                case PolePosition.v3.Common.Enumerations.DataTypes.None:
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueXmission:
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueDistribution:
                case PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueMeter:
                case PolePosition.v3.Common.Enumerations.DataTypes.Custom:
                    break;
                default:
                    break;
            }


            
        }

    }
}
